<?php


namespace app\api\controller;


use app\api\model\Applypartner;
use app\api\model\Applyrule;
use app\api\model\Member;
use app\api\model\Qr;
use think\Db;
use think\Exception;

//http://cosmo.com/api/Apply/submit
//https://cosimo.qswh.net.cn/api/Apply/getOrder

class Apply extends BaseApi {

    public function getApplyRule(){
        $applyRule = new Applyrule();
        $applyRuleItem = $applyRule->find();
        if(empty($applyRuleItem)){
            return json(['code'=>-1,'msg'=>'暂无规则,请联系后台管理员增加规则']);
        }
        return json(['code'=>1,'data'=>$applyRuleItem]);
    }

    //提交申请
    public function submit(){
        if (!$this->request->param('openid')
            || !$this->request->param('username')
            || !$this->request->param('mobile')
            || !$this->request->param('type')   // 2.扫码成为同级销售合伙人(销售合伙人) 3.扫码成为销售合伙人(事业合伙人)
            || !$this->request->param('codeid')
            || !$this->request->param('ruleid')
        ) {
            return json(['code' => -1, 'msg' => '缺少参数']);
        }

        $openid = trim($this->request->param('openid'));
        $username = trim($this->request->param('username'));
        $mobile = trim($this->request->param('mobile'));
        $type = intval($this->request->param('type'));
        $codeid = intval($this->request->param('codeid'));
        $ruleid = intval($this->request->param('ruleid'));

        $applyPartner = new Applypartner();
        $applyPartnerItem = $applyPartner->getApplyItem(['openid'=>$openid,'audit'=>0,'status'=>1]);
        if(!empty($applyPartnerItem)){
            return json(['code'=>-1,'msg'=>'您已经提交了申请,请等待审核']);
        }

        $qr = new Qr();
        $qrItem = $qr->getQrCode(['id'=>$codeid],['memberid','myidentity','openid']);
        if(empty($qrItem)){
            return json(['code'=>-1,'msg'=>'二维码信息不存在或已删除']);
        }

        $applyRule = new Applyrule();
        $applyRuleItem = $applyRule->where('id',$ruleid)->find();
        if(empty($applyRuleItem)){
            return json(['code'=>-1,'msg'=>'暂无规则,请联系后台管理员增加规则']);
        }

        $applysn = 'CSM'.date('YmdHis').$this->random(5);
        $applyPartner->openid = $openid;
        $applyPartner->applysn = $applysn;
        $applyPartner->type = $type;
        $applyPartner->mobile = $mobile;
        $applyPartner->recomopenid = $qrItem->openid;
        $applyPartner->username = $username;
        $applyPartner->applytime = time();
        $applyPartner->createtime = time();

        if($type == 3){//事业合伙人推荐的,成为销售合伙人(不需要支付)
            $applyPartner->refereeid = $qrItem->memberid;
            $applyPartner->status = 3; //不需要支付
        }else{//销售合伙人推荐的,绑定到销售合伙人的上线下
            $member = new Member();
            $levelItem = $member->getMember($qrItem->memberid,['agentid']);
            if(empty($levelItem)){
                return json(['code'=>-1,'msg'=>'该销售合伙人不存在或已删除']);
            }
            $applyPartner->refereeid = $levelItem->agentid;
            $applyPartner->price = $applyRuleItem->money;
        }

        Db::startTrans();
        try{
            $applyPartner->save();
            if(!$applyPartner->id){
                return json(['code'=>-1,'msg'=>'订单生成失败']);
            }

            if($type == 2){
                $payData = [
                    'total_fee' => $applyRuleItem->money,
                    'out_trade_no' => $applysn,
                    'openid' => $openid
                ];
                $payResult = $this->processPay('bindPartner',$payData,'绑定销售合伙人');
                if(empty($payResult)){
                    return ['code'=>-1,'msg'=>'订单支付失败'];
                }
                Db::commit();
                return json(['code'=>1,'data'=>$payResult]);
            }else{
                Db::commit();
                return json(['code'=>1,'msg'=>'申请成功']);
            }
        }catch (Exception $e){
            Db::rollback();
            return json(['code'=>-1,'msg'=>$e->getMessage()]);
        }
    }

    //检测身份
    public function checkIdentity(){
        if (!$this->request->param('openid')) {
            return json(['code' => -1, 'msg' => '缺少参数']);
        }

        $openid = trim($this->request->param('openid'));
        $member = new Member();
        $memberItem = $member->getMember($openid);
        if(empty($memberItem)){
            return json(['code'=>-1,'msg'=>'用户存在或已删除']);
        }
        return json(['code'=>1,'data'=>$memberItem]);
    }

    //已经是会员身份直接绑定
    public function bindIdentity(){
        if (!$this->request->param('openid')
            || !$this->request->param('type')   // 2.扫码成为同级销售合伙人(销售合伙人) 3.扫码成为销售合伙人(事业合伙人)
            || !$this->request->param('codeid')
        ) {
            return json(['code' => -1, 'msg' => '缺少参数']);
        }
        $openid = trim($this->request->param('openid'));
        $type = trim($this->request->param('type'));
        $codeid = trim($this->request->param('codeid'));

        //获取用户身份
        $member = new  Member();
        $memberItem = $member->getMember($openid,['identity']);
        if(empty($memberItem)){
            return json(['code'=>-1,'msg'=>'用户存在或已删除']);
        }

        $qr = new Qr();
        $qrItem = $qr->getQrCode(['id'=>$codeid],['memberid','myidentity']);
        if(empty($qrItem)){
            return json(['code'=>-1,'msg'=>'二维码信息不存在或已删除']);
        }

        if($memberItem->identity < $qrItem->myidentity){
            return json(['code'=>-1,'msg'=>'您的身份是'.$this->getIdentity($memberItem->identity).'不能绑定比您等级低的身份']);
        }

        if($type == 3){//事业合伙人的码
            $memberItem->agentid = $qrItem->memberid;
        }else if($type == 2){//销售合伙人的码
            $levelItem = $member->getMember($qrItem->memberid,['agentid']);
            if(empty($levelItem)){
                return json(['code'=>-1,'msg'=>'该销售合伙人不存在或已删除']);
            }
            $memberItem->agentid = $levelItem->agentid;
        }

        $row = $memberItem->save();
        if($row < 1){
            return json(['code'=>-1,'msg'=>'绑定失败']);
        }

        return json(['code'=>1,'msg'=>'绑定成功败']);

    }

    protected function getIdentity($identity){
        switch ($identity){
            case 1:
                return $identity = '事业合伙人';
                break;
            case 2:
                return $identity = '销售合伙人';
                break;
            case 3:
                return $identity = '会员';
                break;
            case 4:
                return $identity = '自费会员';
                break;
            default:
                return $identity = '其他';
        }
    }

}